package no.saiboten.spotify.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import no.saiboten.spotify.bean.SongBean;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PlaylistService {

	Map<String, Playlist> playlists = new HashMap<String, Playlist>();

	private UserService userService;

	private SpotifySongRestService restService;

	private final Logger LOGGER = Logger.getLogger(getClass());

	@Autowired
	public PlaylistService(UserService userService,
			SpotifySongRestService restService) {
		this.userService = userService;
		this.restService = restService;
	}

	public boolean playlistExists(String playlistId) {
		return playlists.get(playlistId) != null;
	}

	public String createPlaylist(String id) {

		LOGGER.debug("Creating playlist with id: " + id
				+ ". These are the current playlists: " + playlists);
		playlists.put(id, new Playlist(userService, restService, id));
		return id;
	}

	public LinkedList<SongBean> getSongs(String playlistId) {
		return playlists.get(playlistId).getSongs();
	}

	public Map<String, SongBean> getScores(String playlistId) {
		return playlists.get(playlistId).getScores();
	}

	public boolean addSong(String playlistId, String uri) {
		return playlists.get(playlistId).addSong(uri);
	}

	public boolean addPointToSong(String playlistId, String uri) {
		return playlists.get(playlistId).addPointToSong(uri);
	}

	public String getLeadingSongAndResetScore(String playlistId) {
		return playlists.get(playlistId).getLeadingSongAndResetScore();
	}

	public String getLeadingSong(String playlistId) {
		return playlists.get(playlistId).getLeadingSong();
	}
}
